<?php

	class Devmod_method_castField_file extends Devmod_castField_abstraction {
		
		public function type_add()
		{
			$this->view->assign(array(
				'field_name' => $this->cfg['field_name'],
				'value' => '',
				'file' => null,
				'theme' => $this->view->theme,
				'type' => 'add'
			));
			
			$this->view->append('tpl_addons', $this->view->fetch('common/cast_file.tpl'));
			
			return $this->view->fetch('common/cast_file_show.tpl');
		}
		
		
		public function type_edit()
		{
			if($this->value == "0" || $this->value == 0 || empty($this->value)|| is_null($this->value))
			{
				$file = '';
			} else {
				$flhr = new Yomm_Library_FileHandler();
				$file = $flhr->getName($this->value);
				
				if($file === false)
				{
					$file = $flhr->getError();
				}
			}
			
				$this->view->assign(array(
					'field_name' => $this->cfg['field_name'],
					'value' => $this->value,
					'file' => $file,
					'theme' => $this->view->theme,
					'type' => 'edit'
				));
			
			
			$this->view->append('tpl_addons', $this->view->fetch('common/cast_file.tpl'));
			
			return $this->view->fetch('common/cast_file_show.tpl');
		}
		
		public function type_frontend_list()
		{
			if(empty($this->value) || $this->value == "0")
				return 'no_file';
				
			$flhr = new Yomm_Library_FileHandler();
			$file = $flhr->getFile($this->value);
			
			if($file === false)
			{
				return $flhr->getError();
			}
			
			return $file;
		}
		
		public function type_list()
		{
			if(!isset($this->value) || empty($this->value) || $this->value == 0)
				return "no_FILE";
				
			$fhdr = new File();
			$file = $fhdr->getFile($this->value);
			if($file === false)
				return $fhdr->getError();
			
			return "<a href=\"{$file}\" target=\"_blank\">{$fhdr->getName()}</a>";
		}
	}